package 字符串;

public class LC_541反转字符串II {
    public static void main(String[] args){
        String s=s = "abcdefg";
        int k = 2;
        System.out.println(reverseStr(s,k));
    }
    public static String reverseStr(String s, int k) {
        char[] c=s.toCharArray();
        for(int i=0;i<s.length();i+=2*k){
            //k<i<2k
            if(i+k<=s.length()){
                int left=i;
                int right=i+k-1;
                while(left<right){
                    char temp=c[left];
                    c[left]=c[right];
                    c[right]=temp;
                    left++;
                    right--;
                }
            }
            //i<k
            else{
                int left=i;
                int right=s.length()-1;
                while(left<right){
                    char temp=c[left];
                    c[left]=c[right];
                    c[right]=temp;
                    left++;
                    right--;
                }
            }
        }
        s=String.valueOf(c);
        return s;
    }
}
